package com.whfc.ms.api.fvs.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fvs.dto.FvsGroupDTO;
import com.whfc.fvs.param.FvsGroupAddParam;
import com.whfc.fvs.param.FvsGroupEditParam;
import com.whfc.ms.api.fvs.service.MsFvsGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author sun_guodong
 * @description 视频设备分组
 * @date 2020-12-08
 */
@RequiresPermissions("fvs/device")
@RestController
@RequestMapping("/ms/api/fvs/group")
@Validated
public class FvsGroupController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsFvsGroupService fvsGroupService;

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId) {
        logger.info("视频设备分组列表,loginer:{},deptId:{}", user.getUsername(), deptId);
        ListData<FvsGroupDTO> listData = fvsGroupService.list(deptId);
        return ResultUtil.success(listData);
    }

    @GetMapping("/company/list")
    public Result companyList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @RequestParam("deptId") Integer deptId) {
        logger.info("查询公司下全部视频设备分组列表,loginer:{},deptId:{}", user.getUsername(), deptId);
        ListData<FvsGroupDTO> listData = fvsGroupService.companyList(deptId);
        return ResultUtil.success(listData);
    }

    @RequiresPermissions("fvs/group/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @RequestBody FvsGroupAddParam param) {
        logger.info("添加视频设备分组,loginer:{},param:{}", user.getUsername(), param.toString());
        fvsGroupService.add(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("fvs/group/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestBody FvsGroupEditParam param) {
        logger.info("编辑视频设备分组,loginer:{},param:{}", user.getUsername(), param.toString());
        fvsGroupService.edit(param);
        return ResultUtil.success();
    }

    @RequiresPermissions("fvs/group/del")
    @PostMapping("/del/{groupId}")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @PathVariable("groupId") Integer groupId) {
        logger.info("删除视频设备分组,loginer:{},groupId:{}", user.getUsername(), groupId);
        fvsGroupService.del(groupId);
        return ResultUtil.success();
    }

    @GetMapping("/tree")
    public Result tree(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("视频设备分组树,loginer:{},deptId:{},keyword:{}", user.getUsername(), deptId, keyword);
        ListData<FvsGroupDTO> listData = fvsGroupService.tree(deptId, keyword);
        return ResultUtil.success(listData);
    }
}
